package engine.logic
{
	import engine.data.ModelLocator;
	import engine.scene.SceneChangeManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.text.NxLabel;
	
	public class CGLogic
	{
		private var _container			: Sprite;
		
		private var _CG				: MovieClip;
		
		private var _stage				: Stage;
		
		private var _skipBtn			: NxButton;
		
		private var _videoURL			: String = "";
		private var _connection		: NetConnection;
		private var _stream			: NetStream;
		private var _video				: Video;
		private var _videoContainer	: Sprite;
		private var _tipsLabel			: NxLabel;
		
		private var _stopFunc			: Function;
		private var blackMask			:Sprite;
		private var alphaMax:int		=	100;
		private var timer:Timer;
		public function CGLogic( stopFunc : Function , videoUrl:String )
		{
			_videoURL = videoUrl;
			_stopFunc = stopFunc;
			init();
			initFlv();
		}
		
		private function initFlv():void
		{
			_connection = new NetConnection();
			_connection.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
			_connection.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
			_connection.connect(null);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void 
		{
			switch (event.info.code) 
			{
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
//					trace( "Stream not found: " + _videoURL );
					break;
				case "NetStream.Play.Stop":
					clear();
					break;
			}
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void 
		{
//			trace("securityErrorHandler: " + event);
		}
		
		private function connectStream():void 
		{
			_stream = new NetStream( _connection );
			_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			_stream.client = new CustomClient();
			_video = new Video();
			_video.attachNetStream( _stream );
			_stream.play( getUrl() );
			_videoContainer.addChild(_video);
			_video.width = 1280;
			_video.height = 768;
			
			onStageResize();
		}
		
		private function getUrl():String
		{
			_videoURL = Jxjh.getUrl(_videoURL)
			
			var cdn : String = ModelLocator.getInstance().mainIni.getContent("CDN","path");
			if(cdn != "")
			{
				_videoURL	=	cdn + _videoURL;
			}
			
			return _videoURL;
		}

		
		private function init():void
		{
			blackMask	=		new Sprite();
			_stage	=	StageInfo.stage;
			timer	=	new Timer(100);
			timer.addEventListener(TimerEvent.TIMER,timerHandle)
			_stage.addEventListener(Event.RESIZE, onStageResize, false, 10);
			
			_container = new Sprite();
//			_container.x = ( 1280 - _stage.fullScreenWidth ) * .5;
//			_container.y = ( 768 - _stage.fullScreenHeight ) * .5;
			StageInfo.systemSpace.addChild( _container );
			blackMask.graphics.beginFill(0xFFFFFF);
			blackMask.graphics.drawRect(-1280, -768, 1280*3, 768*3);
			blackMask.graphics.endFill();
			blackMask.alpha	=	0;
			blackMask.mouseChildren = blackMask.mouseEnabled =false;
//			var maskUI : Sprite = new Sprite();
//			maskUI.graphics.beginFill( 0, 1 );
//			maskUI.graphics.drawRect( 0, -200, _stage.fullScreenWidth + 100, _stage.fullScreenHeight + 300 );
//			maskUI.graphics.endFill();
//			_container.addChild( maskUI );
//			maskUI.y = - 250;
			
			_videoContainer = new Sprite();
			_container.addChild( _videoContainer );
		
			_skipBtn = new NxButton();
			_skipBtn.skin = "Button_2";
			_skipBtn.label = StaticIni.getSwfLangStr( "UIStr_1000406" );
			_skipBtn.mouseChildren = _skipBtn.mouseEnabled = _skipBtn.visible = true;
			_skipBtn.addEventListener( MouseEvent.CLICK, onSkipHandle );
			_container.addChild( _skipBtn );
			
			_tipsLabel = new NxLabel();
			_tipsLabel.htmlStrByBorder = StaticIni.getSwfLangStr( "UIStr_Load_Movie" );
			_container.addChild( _tipsLabel );
			_container.addChild(blackMask)
			onStageResize();
		}
		
		private function timerHandle(e:TimerEvent):void 
		{
			if (alphaMax <= 0) 
			{
				blackMask.alpha = 0;
				if (blackMask.parent) 
				{
					blackMask.parent.removeChild(blackMask);
				}
				timer.stop();
				clearAll();
			}
			else
			{
				blackMask.alpha	=	alphaMax / 100;
				alphaMax -= 10;
			}
		}
		
		private function onSkipHandle( evt : MouseEvent ):void
		{
			clear();
		}
		
		private function onStageResize( evt : Event = null ):void
		{
			var xPoint : int = SceneChangeManager.attackSceneViewX;
			var yPoint : int = SceneChangeManager.attackSceneViewY;
			
			if( null != _video )
			{	
				_video.x = xPoint;
				_video.y = yPoint;
			}
			
			_skipBtn.x = xPoint + 1180;
			_skipBtn.y = yPoint + 718;
			
			_tipsLabel.x = xPoint + ( 1280 - _tipsLabel.width ) * .5;
			_tipsLabel.y = yPoint;
		}
		
		private function clear():void
		{
			blackMask.alpha = 1
			alphaMax = 100;
			_connection.close();
			_stream.close();
			_video.clear();
			
			if( _container.parent != null )
			{
				_skipBtn.parent.removeChild(_skipBtn);
				_tipsLabel.parent.removeChild(_tipsLabel);
			}
			if( null != _video )
			{
				_video.clear();
				
				if(  _video.parent != null )
				{
					_video.parent.removeChild( _video );
				}
			}
			
			
			_stage.removeEventListener(Event.RESIZE, onStageResize);
			_skipBtn.removeEventListener( MouseEvent.CLICK, onSkipHandle );
			timer.start();
		}
		private function clearAll():void
		{	
			_video = null;
			if (_container.parent) 
			{
				_container.parent.removeChild(_container);
			}
			_stopFunc.call();
		}
//		/**
//		 *	加载cg动画 
//		 * 
//		 */		
//		private function loadingCG():void
//		{
//			SwfLoader.StartLoad( "res/swf/CG.swf", null, onComplete, [ onLoadingHandle ] );	
//			
//			LoadingLogic.getInstance().setProcess( 2, _tips );
//		}
		
//		private function onComplete( val:DisplayObject, pList:Array ):void
//		{
//			_CG = val as MovieClip;
//			onStageResize();
//			
//			_container.addChild( val );
//			
//			LoadingLogic.getInstance().setProcess(100);
//			
//			StageInfo.systemSpace.addChild( _container );
//		}
		
//		private function onLoadingHandle(event:Event):void
//		{
//			if(event.type == ProgressEvent.PROGRESS)
//			{
//				var e:ProgressEvent	=	event as ProgressEvent;
//				LoadingLogic.getInstance().setProcess( e.bytesLoaded*100 / e.bytesTotal );
//			}
//		}	 
	}
}


class CustomClient {
	public function onMetaData(info:Object):void {
//		trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
	}
	public function onCuePoint(info:Object):void {
//		trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
	}
	public function onXMPData(info:Object):void {
//		trace("XMPData: data=" + info.liveXML) ;
	}
}

